﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace School.SystemManage.WebPages.UserManage
{
    public partial class Role : Base.PermissionBasePage
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            var roles = db.Role;
            lbxRole.DataSource = roles;
            lbxRole.DataTextField = "RoleName";
            lbxRole.DataValueField = "RID";
            lbxRole.DataBind();
          
            //string sql = "SELECT Value a FROM TechnologySchoolEntities.AdimMenu as a WHERE a.mid in {" + role.Permissions + "}";
            //var menu = db.CreateQuery<AdimMenu>(sql);
        }

        protected void lbxRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckBoxList1.Items.Clear();
            GetRolePre();
        }

        private void GetRolePre()
        {
            int rid = int.Parse(lbxRole.SelectedValue);
            var role = db.Role.FirstOrDefault(r => r.RID == rid);

            //获取权限模块
            if (!string.IsNullOrEmpty(role.Permissions))
            {
                string[] pers = role.Permissions.Split(',');
                var menu = db.AdimMenu.Where(m => m.parentId == 0);
                foreach (var m in menu)
                {
                    ListItem li = new ListItem();
                    if (pers.Contains(m.mid.ToString()))
                    {
                        li.Selected = true;
                    }
                    li.Text = m.name;
                    li.Value = m.mid.ToString();

                    CheckBoxList1.Items.Add(li);
                }
            }
            else 
            {
                var menu = db.AdimMenu.Where(m => m.parentId == 0);
                CheckBoxList1.DataSource = menu;
                CheckBoxList1.DataTextField = "name";
                CheckBoxList1.DataValueField = "mid";
                CheckBoxList1.DataBind();
            }

            //获取操作权限
            if (!string.IsNullOrEmpty(role.Action))
            {
                char[] actions = role.Action.ToCharArray();
                for (int i = 0; i < actions.Length; i++)
                {
                    cbl_Action.Items[i].Selected = actions[i] == char.Parse("1");
                }
            }
            else
            {
                foreach (ListItem li in cbl_Action.Items)
                {
                    if (li.Selected)
                    {
                        li.Selected = false; ;
                    }
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (lbxRole.SelectedItem != null)
            {
                int rid = int.Parse(lbxRole.SelectedValue);
                var role = db.Role.FirstOrDefault(r => r.RID == rid);

                db.DeleteObject(role);

                if (db.SaveChanges() > 0)
                {
                    lblMsg.Text = "删除成功！";
                    BindData();
                }
                else
                {
                    lblMsg.Text = "删除失败！";
                }
            }
            else
            {
                lblMsg.Text = "请先选中要删除的项！";
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //设定模块权限
            IList<string> pers = new List<string>();
            foreach (ListItem li in CheckBoxList1.Items)
            {
                if (li.Selected)
                {
                    pers.Add(li.Value);
                }
            }
            string persStr = string.Join(",", pers.ToArray());

            int rid = int.Parse(lbxRole.SelectedValue);
            var role = db.Role.FirstOrDefault(r => r.RID == rid);
            role.Permissions = persStr;

            //设定操作权限
            string actions = string.Empty;
            foreach (ListItem li in cbl_Action.Items)
            {
                actions += li.Selected == true ? "1" : "0";
            }
            role.Action = actions;

            if (db.SaveChanges() > 0)
            {
                lblMsg.Text = "设定成功！";
            }
            else
            {
                lblMsg.Text = "设定失败！";
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
           School.Role newRole = new School.Role();
           newRole.RoleName = txbRoleName.Text.Trim();

           db.AddToRole(newRole);

           if (db.SaveChanges() > 0)
           {
               lblMsg.Text = "添加成功！";
               BindData();
           }
           else
           {
               lblMsg.Text = "添加失败！";
           }
        }
    }
}
